<?php
/**
 * RabbitCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@tomatocms.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2009-2010 TIG Corporation (http://www.tig.vn)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @version 	$Id: PrivilegeController.php 4517 2010-08-12 09:35:24Z huuphuoc $
 * @since		2.0.0
 */

class Core_PrivilegeController extends Zend_Controller_Action 
{
	/* ========== Backend actions =========================================== */

	/**
	 * Add new privilege
	 * 
	 * @return void
	 */
	public function addAction() 
	{
		$this->_helper->getHelper('viewRenderer')->setNoRender();
		$this->_helper->getHelper('layout')->disableLayout();
		
		$request = $this->getRequest();
		if ($request->isPost()) {
			$mca 		 = $request->getPost('mca');
			$description = $request->getPost('description');
			list($module, $controller, $action) = explode(':', $mca);
			
			$privilege = new Core_Models_Privilege(array(
				'name' 			  => $action,
				'description' 	  => $description,
				'module_name' 	  => $module,
				'controller_name' => $controller,
			));
			$conn = Rabbit_Db_Connection::factory()->getMasterConnection();
			$privilegeModel = Rabbit_Model_Model_Factory::getInstance()->setModule('core')->getPrivilegeModel();
			$privilegeModel->setDbConnection($conn);
			$id = $privilegeModel->add($privilege);
			
			$this->getResponse()->setBody($id);
		}
	}
	
	/**
	 * Delete privilege
	 * 
	 * @return void
	 */
	public function deleteAction() 
	{
		$this->_helper->getHelper('viewRenderer')->setNoRender();
		$this->_helper->getHelper('layout')->disableLayout();
		
		$request = $this->getRequest();
		if ($request->isPost()) {
			$id = $request->getPost('id');
			$privilegeModel = Rabbit_Model_Factory::getInstance()->setModule('core')->getPrivilegeModel();
			$ruleModel 	  = Rabbit_Model_Factory::getInstance()->setModule('core')->getRuleModel();
			
			$privilege = $privilegeModel->getById($id);
			$data = array(
				'mca' 		  => implode(array($privilege->module_name, $privilege->controller_name, $privilege->name), ':'), 
				'description' => $privilege->description,
			);
			$privilegeModel->delete($id);
			
			/**
			 * Remove from rule
			 */
			$ruleModel->removeRules($id);
			
			$this->getResponse()->setBody(Zend_Json::encode($data));
		}
	}
	
	/**
	 * List privileges
	 * 
	 * @return void
	 */
	public function listAction() 
	{
          $modules = Rabbit_Module_Loader::getInstance()->getModuleNames();
		//$modules = Rabbit_Utility_File::getSubDir(RABBIT_APP_DIR . DS . 'modules');
	
		$resourceModel  = Rabbit_Model_Factory::getInstance()->setModule('core')->getResourceModel();
		$privilegeModel = Rabbit_Model_Factory::getInstance()->setModule('core')->getPrivilegeModel();
		
		/**
		 * Get resources
		 */
		$resources = $resourceModel->getResources();
		$dbResources = array();
		if ($resources) {
			foreach ($resources as $resource) {
				$dbResources[$resource->module_name . ':' . $resource->controller_name] = $resource->id;
			}
		}
		
		/**
		 * Get privileges
		 */
		$privileges = $privilegeModel->getPrivileges();
		$dbPrivileges = array();
		if ($privileges) {
			foreach ($privileges as $privilege) {
				$dbPrivileges[$privilege->module_name . ':' . $privilege->controller_name . ':' . $privilege->name] = $privilege->id;
			}
		}
		
		$this->view->assign('modules', $modules);
		$this->view->assign('dbResources', $dbResources);
		$this->view->assign('dbPrivileges', $dbPrivileges);				
	}
}
